package cn.green.core.excel.model;

import cn.green.core.components.field.FieldCheckTypeCom;
import cn.green.core.components.store.field.FieldCheckTypeStore;
import cn.green.core.model.QField;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className Column
 * @description excel表格
 * @date 2025/1/3 11:09
 */
@Data
public abstract class Column {

    /**
     * xls的单元格风格
     */
    @Setter
    @Getter
    protected HSSFCellStyle xlsTitleCellStyle;

    /**
     * xlsx的单元格风格
     */
    @Setter
    @Getter
    protected XSSFCellStyle xlsxTitleCellStyle;

    public Column(Integer index, QField field) {
        this.columnName = field.getAliasName();
        //this.defaultValue = field.getDefaultValue() == null ? "" : field.getDefaultValue();
        this.field = field;
        this.maxRow = 65535;
        this.index = index;
        List<String> fieldCheck = field.getFieldCheck();
        if (fieldCheck != null) {
            for (String fieldCheckKey : fieldCheck) {
                FieldCheckTypeCom fieldCheckTypeCom = FieldCheckTypeStore.get(fieldCheckKey);
                if (fieldCheckTypeCom != null) {
                    if (fieldCheckTypeCom.required()) {
                        this.columnName = field.getAliasName() + "*";
                    }
                }
            }
        }
    }

    public Map<String, String> columnFieldMaps() {
        Map<String, String> rtn = new HashMap<>();
        rtn.put(this.columnName, this.field.getFieldName());
        return rtn;
    }



    /**
     * 列位置
     */
    @Setter
    @Getter
    protected Integer index;

    /**
     * 最大行数
     */
    protected Integer maxRow;

    /**
     * 列名
     */
    protected String columnName;

    /**
     * 列的默认值 没有用处了，在导入时通过dml组件设置默认值
     */
    @Deprecated
    protected Object defaultValue;

    /**
     * 该列所属的field
     */
    protected QField field;

    public abstract List<HSSFCell> generateXlsTitle(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row);

    public abstract List<XSSFCell> generateXlsxTitle(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row);

    /**
     * 生成xls的单元格
     * @param row
     */
    public abstract List<HSSFCell> generateXlsCell(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, Object cellVal);

    /**
     * 生成xlsx的单元格
     * @param row
     */
    public abstract List<XSSFCell> generateXlsxCell(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, Object cellVal);

    /**
     * 从xls单元格获取数据
     * @param cells 属于该字段组件的单元格 key:fieldName, val:cell
     * @return 数据
     */
    public abstract Map<String, Object> getValueFromXlsCell(Map<String, HSSFCell> cells);

    /**
     * 从xlsx单元格获取数据
     * @param cells 属于该字段组件的单元格 key:fieldName, val:cell
     * @return  数据
     */
    public abstract Map<String, Object> getValueFromXlsxCell(Map<String, XSSFCell> cells);

}
